﻿using CoreModule;
using Prism.Ioc;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Media.Imaging;
using ToastNotifications;
using ToastNotifications.Lifetime;
using ToastNotifications.Messages;
using ToastNotifications.Position;
using WPFLocalizeExtension.Engine;
using Yuwell.BloodGlucose.BluetoothDataParser.App.ViewModels;
using Yuwell.TunnelTimeSphygmomanometer.App;
namespace Yuwell.BloodGlucose.BluetoothDataParser.App.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window, INotifier
    {

        public static int DataCountPerPage=10;
        IContainerProvider ContainerProvider { get; set; }
      
        Notifier notifier = new Notifier(cfg =>
        {
            cfg.DisplayOptions.Width = 600;
            cfg.PositionProvider = new WindowPositionProvider(
                parentWindow: Application.Current.MainWindow,
                corner: ToastNotifications.Position.Corner.BottomCenter,
               
                offsetX: 0,
                offsetY: 30);
            cfg.LifetimeSupervisor = new TimeAndCountBasedLifetimeSupervisor(
                notificationLifetime: TimeSpan.FromSeconds(3),
                maximumNotificationCount: MaximumNotificationCount.FromCount(1));
            cfg.Dispatcher = Application.Current.Dispatcher;
        });

        IRegionManager RegionManager { get; set; }
        WelcomeWindow WelcomeWindow { get; set; }

        /// 
        /// </summary>
        /// <param name="containerProvider"></param>
        /// <param name="regionManager"></param>
        public MainWindow(IContainerProvider containerProvider)
        {
            InitializeComponent();
            ContainerProvider = containerProvider;
            WelcomeWindow = new WelcomeWindow();
            WelcomeWindow.Show();

        }
  
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNormal_Click(object sender, RoutedEventArgs e)
        {

            if (this.WindowState == WindowState.Normal)
            {


                this.topRegion.Visibility = Visibility.Visible;
                this.WindowState = WindowState.Maximized;
                imgNormal.Source = new BitmapImage(new Uri("pack://application:,,,/Images/to-normal.png"));
            }
            else
            {

                this.WindowState = WindowState.Normal;
                imgNormal.Source = new BitmapImage(new Uri("pack://application:,,,/Images/to-max.png"));
            }

        }
        private void btnToFull_Click(object sender, RoutedEventArgs e)
        {

            this.topRegion.Visibility = Visibility.Collapsed;
            this.WindowState = WindowState.Maximized;

            this.ShowWarning("按 Esc 即可退出全屏模式");

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMinimize_Click(object sender, RoutedEventArgs e)
        {
     
            this.WindowState = WindowState.Minimized;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {

            if (e.Key == System.Windows.Input.Key.Escape
                && this.topRegion.Visibility == Visibility.Collapsed)
            {
                this.topRegion.Visibility = Visibility.Visible;
            }
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void topRegion_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            e.Handled = true;
        }
        private void topRegion_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            e.Handled = true;
        }
        private void topRegion_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            e.Handled = true;
        }
        private void topRegion_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            e.Handled = true;
        }
        public bool IsShowConfigurationHome = false;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
        }


        public List<BloodGlucoseMeasurementInfo> GlucoseMeasurementInfos = new List<BloodGlucoseMeasurementInfo>();


        Style DefaultPaginationButtonStyle { get; set; } = default(Style);
         
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            WelcomeWindow.Close();
            var containerRegistry = ContainerProvider.Resolve<IContainerRegistry>();

            containerRegistry.RegisterInstance<FrameworkElement>(this.mask, CoreConstant.MaskKey);
            ///


            LocalizeDictionary.Instance.SetCurrentThreadCulture = true;
            LocalizeDictionary.Instance.Culture = new CultureInfo("zh");
             
            var vm = this.DataContext as MainWindowViewModel;

            if (vm != null)
            {
                var usingResLanguage = App.UsingResLanguage;
                vm.UseResource(usingResLanguage);
            }

        }

        public void ShowInformation(string message)
        {
            notifier.ClearMessages(new DefaultClearStrategy());
           
          notifier.ShowInformation(message, new ToastNotifications.Core.MessageOptions() { ShowCloseButton = false });
        }
        public void ShowSuccess(string message)
        {
            notifier.ClearMessages(new DefaultClearStrategy());
            notifier.ShowSuccess(message, new ToastNotifications.Core.MessageOptions() { ShowCloseButton=false});
        }
        public void ShowWarning(string message)
        {
            notifier.ClearMessages(new DefaultClearStrategy());
            notifier.ShowWarning(message, new ToastNotifications.Core.MessageOptions() { ShowCloseButton = false });
        }
        public void ShowError(string message)
        {
            notifier.ClearMessages(new DefaultClearStrategy());
            notifier.ShowError(message, new ToastNotifications.Core.MessageOptions() { ShowCloseButton = false });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {

          notifier.Dispose();
             
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
 
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, EventArgs e)
        {
            

        }
  
        private void Button_Click(object sender, RoutedEventArgs e)
        {
     
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Normal)
            {

                imgNormal.Source = new BitmapImage(new Uri("pack://application:,,,/Images/to-max.png"));

              
            }
            else
            {

                imgNormal.Source = new BitmapImage(new Uri("pack://application:,,,/Images/to-normal.png"));

            }

        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {

        }
    }
}

